﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace SHCP
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("测试价格和数字");
            var clientCLT = new CLT(App.Default.WinName);

            if (clientCLT.Window == IntPtr.Zero)
            {
                Console.WriteLine("客户端未启动");
                Console.ReadKey();
            }

            clientCLT.Window.FindAllChilds("TEdit")
                .ToList()
                .ForEach(p =>
                {
                    Win32API.SetForegroundWindow(p);
                    SendKeys.SendWait("{BS 6}");
                    SendKeys.SendWait("sdddff".ToString());
                });

            Console.ReadKey();


            /////如果秒杀启动
            //if (App.Default.MSEnable > 0)
            //{
            //    Console.WriteLine("{0}:车牌秒杀程序启动!", DateTime.Now.ToString());
            //    if (clientCLT.Price <= 0)
            //    {
            //        Console.WriteLine("无法获取价格!");
            //        Console.ReadKey();
            //    }
            //    clientCLT.ChuJia(true);
            //    InputValidateCodeAndConfirm(clientCLT);
            //    Console.ReadKey();
            //}

            ////监听验证码
            //Console.WriteLine("{0}:监控程序启动", DateTime.Now.ToString());
            //while (true)
            //{
            //    System.Threading.Thread.Sleep(500);
            //    var clientYZM = new YZM(App.Default.WinName);
            //    if (clientYZM.Window == IntPtr.Zero ||
            //        clientYZM.Rect.Size.Width - clientYZM.Rect.Left != App.Default.YZMWidth ||
            //        clientYZM.Rect.Size.Height - clientYZM.Rect.Top != App.Default.YZMHeight)
            //    {
            //        Console.WriteLine("未发现验证码窗体");
            //        continue;
            //    }
            //    InputValidateCodeAndConfirm(clientCLT);
            //    break;
            //}
        }


        private static int MaxReTry = 5;//最多尝试5次
        static void InputValidateCodeAndConfirm(CLT clientCLT)
        {
            DirectoryInfo info = new DirectoryInfo(Consts.DataFloderVCode);
            info.GetFiles().ToList().ForEach(f => f.Delete());
            int retryTime = 1;
            bool isSuccess = false;

            while (retryTime <= MaxReTry)
            {
                retryTime++;
                Console.WriteLine("启动新一轮的验证码识别");
                var clientYZM = new YZM(App.Default.WinName);
                if (clientYZM.Window == IntPtr.Zero)
                {
                    Console.WriteLine("未发现验证码");
                    continue;
                }

                var code = string.Empty;

                clientYZM.Active();

                if (!clientYZM.TryGetValidateCode(out code))
                {
                    Console.WriteLine("未能正确识别号码:{0}", code);
                    clientYZM.Esc();
                    clientCLT.ChuJia(false);//仅仅重新出价
                    continue;
                }

                Console.WriteLine("当前验证码为:{0}", code);

                ///模拟输入验证码
                SendKeys.SendWait(code);

                ///模拟点击确认
                clientYZM.Confirm();

                isSuccess = true;

                break;
            }

            if (isSuccess && App.Default.MSEnable > 0)
                Console.WriteLine("{0}恭喜！成功秒杀!", DateTime.Now.ToString());
        }
    }
}
